#include "Klass.h"
#include "Student.h"
#include "Person.h"
#include <string>
#include <iostream>

using namespace std;

Klass::Klass(size_t number):number(number)
{

}

std::string operator+(const std::string &lhs, const Klass &klass)
{
    return lhs + to_string(klass.number);
}

bool Klass::operator==(const Klass &klass) const 
{
    return klass.number == this->number;
}

void Klass::AssignLeader(Student &student)
{
    if (this->HasMember(student)) {
        this->leader = &student;
    }else{
        cout << "It is not one of us.";
    }
}

bool Klass::HasLeader(Student &student)
{
    // return this->leader != nullptr && (*(Person *)(this->leader) == *(Person *)&student);
    return this->leader != nullptr && student == * (this->leader);
    // return this->leader != nullptr && student.IsEqual(* (this->leader));
}

bool Klass::HasMember(Student &student)
{
    return find_if(this->members.begin(), this->members.end(),
                   [student](Student *member) { return *member == student; }) != this->members.end();
}


void Klass::AppendMember(Student &student) 
{
    if (!this->HasMember(student)) {
        this->members.push_back(&student);
        student.ReAssignKlass(*this);
        for (auto *observer : this->observers) {
            observer->notify(student);
        }
    }

}

void Klass::RegisterObserver(const KlassObserver &observer) 
{
    this->observers.push_back(&observer);
}
